package com.zygk.library.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.flyco.roundview.RoundTextView;
import com.zygk.library.R;
import com.zygk.library.R2;
import com.zygk.library.adapter.HeaderAdAdapter;
import com.zygk.library.base.BaseImageActivity;
import com.zygk.library.model.M_Ad;
import com.zygk.library.util.DensityUtil;
import com.zygk.library.util.ImageManager;
import com.zygk.library.util.StringUtils;
import java.util.ArrayList;
import java.util.List;

public class HeaderAdViewView extends HeaderViewInterface<List<M_Ad>> {
  @BindView(R2.id.vp_ad) ViewPager vpAd;
  @BindView(R2.id.ll_index_container) LinearLayout llIndexContainer;
  @BindView(R2.id.tv_pic_num) RoundTextView tvPicNum;

  private static final int TYPE_CHANGE_AD = 0;

  public static final int TYPE_INDICATOR_DOT = 110;
  public static final int TYPE_INDICATOR_NUM = 120;


  private int indicator_type = TYPE_INDICATOR_DOT;

  private Thread mThread;
  private List<ImageView> ivList;
  private List<String> ivurlList = new ArrayList<String>();
  private boolean isStopThread = false;
  private ImageManager mImageManager;
  private int height;
  private HeaderAdAdapter photoAdapter;

  private int showType = 1;  //0  无操作，1  网页  2   跳转到图片展示  3 自定义
  private boolean rotate = true;
  private ImageView.ScaleType scaleType = ImageView.ScaleType.CENTER_CROP;

  private Handler mHandler = new Handler() {
    @Override public void handleMessage(Message msg) {
      super.handleMessage(msg);
      if (msg.what == TYPE_CHANGE_AD) {
        vpAd.setCurrentItem(vpAd.getCurrentItem() + 1);
      }
    }
  };

  public HeaderAdViewView(Activity context) {
    super(context);
    ivList = new ArrayList<>();
    mImageManager = new ImageManager(context);
  }

  public HeaderAdViewView(Activity context, int height) {
    super(context);
    ivList = new ArrayList<>();
    mImageManager = new ImageManager(context);
    this.height = height;
  }

  public void showNum() {
    indicator_type = TYPE_INDICATOR_NUM;
  }

  public void setShowType(int showType) {
    this.showType = showType;
  }

  public void setScaleType(ImageView.ScaleType scaleType) {
    this.scaleType = scaleType;
  }

  public void setRotate(boolean rotate) {
    this.rotate = rotate;
  }

  @Override protected void getView(List<M_Ad> list, ViewGroup viewGroup) {
    View view = mInflate.inflate(R.layout.library_header_ad_layout, viewGroup, false);
    if (height > 0) {
      if (viewGroup instanceof ListView) {
        AbsListView.LayoutParams lp = (AbsListView.LayoutParams) view.getLayoutParams();
        lp.height = height;
        view.setLayoutParams(lp);
      } else {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
        lp.height = height;
        view.setLayoutParams(lp);
      }
    }
    ButterKnife.bind(this, view);
    dealWithTheView(list);

    if (viewGroup instanceof ListView) {
      ListView listView = (ListView) viewGroup;
      listView.addHeaderView(view);
    } else {
      viewGroup.addView(view);
    }
  }

  private void dealWithTheView(List<M_Ad> list) {
    if(list == null || list.isEmpty())
      return;

    ivList.clear();
    ivurlList.clear();
    int size = list.size();
    for (int i = 0; i < size; i++) {
      ivList.add(createImageView(list.get(i), i));
      ivurlList.add(list.get(i).getPic());
    }
    photoAdapter = new HeaderAdAdapter(mContext, ivList);
    vpAd.setAdapter(photoAdapter);
    addIndicatorImageViews(size);
    setViewPagerChangeListener(size);
    if (rotate) startADRotate();
  }

  public void setData(List<M_Ad> list) {
    if (rotate) stopADRotate();

    dealWithTheView(list);
  }

  // 创建要显示的ImageView
  private ImageView createImageView(final M_Ad mAd, final int postion) {
    ImageView imageView = new ImageView(mContext);
    AbsListView.LayoutParams params =
        new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    imageView.setLayoutParams(params);
    //imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setScaleType(scaleType);
    if (showType == 1) {//网页
      imageView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          //IntentUtil.start_activity(mContext, BaseWebViewActivity.class,
          //        new Pair<String, String>("titlename", mAd.name), new Pair<String, String>("url", mAd.getUrl()));
        }
      });
    } else if (showType == 2 && !StringUtils.isBlank(mAd.getUrl())) {//打开图片展示
      imageView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          //AppUtils.toImageDetial(mContext, postion, ivurlList, false);
          Intent intent = new Intent(mContext, BaseImageActivity.class);
          intent.putExtra(BaseImageActivity.INTENT_TITLE, mAd.getName());
          intent.putExtra(BaseImageActivity.INTENT_URL, mAd.getUrl());
          mContext.startActivity(intent);
        }
      });
    } else if (showType == 3) {
      imageView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          if (imageOnClick != null) imageOnClick.imageOnclick();
        }
      });
    }

    mImageManager.loadUrlImage(mAd.getPic(), imageView);
    return imageView;
  }

  // 添加指示图标
  private void addIndicatorImageViews(int size) {
    // 只有一张图片时不显示指示器
    //        if (size == 1) {
    //            llIndexContainer.setVisibility(View.GONE);
    //            tvPicNum.setVisibility(View.GONE);
    //            return;
    //        }
    if (indicator_type == TYPE_INDICATOR_DOT) {
      llIndexContainer.setVisibility(View.VISIBLE);
      tvPicNum.setVisibility(View.GONE);
      llIndexContainer.removeAllViews();
      for (int i = 0; i < size; i++) {
        ImageView iv = new ImageView(mContext);
        LinearLayout.LayoutParams lp =
            new LinearLayout.LayoutParams(DensityUtil.dip2px(mContext, 5),
                DensityUtil.dip2px(mContext, 5));
        if (i != 0) {
          lp.leftMargin = DensityUtil.dip2px(mContext, 7);
        }
        iv.setLayoutParams(lp);
        iv.setBackgroundResource(R.drawable.library_xml_round_blue_grey_sel);
        iv.setEnabled(false);
        if (i == 0) {
          iv.setEnabled(true);
        }
        llIndexContainer.addView(iv);
      }
    } else if (indicator_type == TYPE_INDICATOR_NUM) {
      llIndexContainer.setVisibility(View.GONE);
      tvPicNum.setVisibility(View.VISIBLE);
      setNum(1);
    }
  }

  public void setNum(int currentNum) {
    tvPicNum.setText(currentNum + "/" + ivList.size());
  }

  // 为ViewPager设置监听器
  private void setViewPagerChangeListener(final int size) {
    vpAd.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override public void onPageSelected(int position) {
        if (ivList != null && ivList.size() > 0) {
          int newPosition = position % size;

          if (indicator_type == TYPE_INDICATOR_DOT) {
            for (int i = 0; i < size; i++) {
              llIndexContainer.getChildAt(i).setEnabled(false);
              if (i == newPosition) {
                llIndexContainer.getChildAt(i).setEnabled(true);
              }
            }
          } else if (indicator_type == TYPE_INDICATOR_NUM) {
            setNum(newPosition + 1);
          }
        }
      }

      @Override public void onPageScrolled(int position, float arg1, int arg2) {
      }

      @Override public void onPageScrollStateChanged(int arg0) {
      }
    });
  }

  // 启动循环广告的线程
  private void startADRotate() {
    isStopThread = false;
    // 一个广告的时候不用转
    if (ivList == null || ivList.size() <= 1) {
      return;
    }
    if (mThread == null) {
      mThread = new Thread(new Runnable() {
        @Override public void run() {
          // 当没离开该页面时一直转
          while (!isStopThread) {
            // 每隔4秒转一次
            SystemClock.sleep(4000);
            // 在主线程更新界面
            mHandler.sendEmptyMessage(TYPE_CHANGE_AD);
          }
        }
      });
      mThread.start();
    }
  }

  // 停止循环广告的线程，清空消息队列
  public void stopADRotate() {
    isStopThread = true;
    if (mHandler != null && mHandler.hasMessages(TYPE_CHANGE_AD)) {
      mHandler.removeMessages(TYPE_CHANGE_AD);
    }
  }

  public interface ImageOnClick {
    void imageOnclick();
  }

  private ImageOnClick imageOnClick;

  public void setImageOnClick(ImageOnClick imageOnClick) {
    this.imageOnClick = imageOnClick;
  }
}

